Control your Nanoleaf devices with Swift.
- iOS 15.0+
- macOS 12.0+
In Xcode:
- Click
Project
. - Click
Package Dependencies
. - Click
+
. - Enter package URL:
https://github.com/reddavis/Nanoleaf
. - Add
Nanoleaf
to your app target.
Documentation can be found here.
To connect to a Nanoleaf device, three steps are required.
Firstly use NanoleafDeviceBrowser
to discover local devices with Bonjour. The NanoleafDeviceBrowser
will return an array of DeviceIdentifier
's.
let browser = NanoleafDeviceBrowser()
for await identifiers in browser.identifiers {
// ...
}
If you want some useful utilties and extensions for using
AsyncSequence
check out Asynchrone.
Next, you will need to resolve the DeviceIdentifier
into a DeviceAddress
. This can be done using the aptly named DeviceAddressResolver
.
let resolver = DeviceAddressResolver(identifier: identifier)
do {
let address = try await resolver.resolve()
// ...
} catch {
// ...
}
Finally, you need to make a authentification request. Before executing this request, press and hold the power button on the Nanoleaf device untill the LEDs begin to flash. Now execute the AuthenticateRequest
using the url
of the DeviceAddress
. If successful, this will return an Auth
object that contains a token.
do {
let client = NanoleafClient.main()
let request = AuthenticateRequest(url: address.url)
let response = try await client.execute(request: request)
let token = response.token
} catch {
// ...
}